///////////////////////////////////////////////////
/// toto Code
/// $Id: cond.h 3 2009-12-14 12:46:10Z tonyjobmails $
/// $Date: 2009-12-14 12:46:10 +0000 (Mon, 14 Dec 2009) $
/// $Author: tonyjobmails $
/// $Revision: 3 $
/// E-mail: tonyjobmails@gmail.com
/// filename: cond.h
/// function: 条件变量定义
///////////////////////////////////////////////////

#ifndef __COND__H
#define __COND__H

#include <pthread.h>

namespace toto
{
	class xcond
	{
		public:
			xcond(const int kind = PTHREAD_PROCESS_PRIVATE)
			{
				pthread_condattr_t attr;
				pthread_condattr_init(&attr);
				pthread_condattr_setpshared(&attr, kind);
				pthread_cond_init(&this->cond, &attr);
				/// pthread_condattr_destroy(&attr);
			}
			
			~xcond()
			{
				pthread_cond_destroy(&this->cond);
			}
			
			/// 等待条件
			void wait(xmutex &mutex)
			{
				pthread_cond_wait(&this->cond, &mutex.mutex);
			}
			
			/// 条件满足，激活第一个线程
			void signal()
			{
				pthread_cond_signal(&this->cond);
			}
			
			/// 激活队列中全部线程
			void broadcast()
			{
				pthread_cond_broadcast(&this->cond);
			}
			
		private:
			/// 条件变量
			pthread_cond_t cond;
	};
};

#endif	/// __COND__H